<?php
Globals::requireController('Admin');
Globals::requireTable('Trysee');
Globals::requireTable('Tryseetype');
Globals::requireTable('Tryseeorder');

class TryseeController extends AdminController
{
    protected $TryseeTable;
    protected $TryseeTypeTable;
    protected $TryseeOrderTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->TryseeTable = new TryseeTable($config);
		$this->TryseeTypeTable = new TryseetypeTable($config);
		$this->TryseeOrderTable = new TryseeorderTable($config);
	}
	
	public function indexAction()
	{
	    $where = array('status'=>1);
	    $count = $this->TryseeTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->TryseeTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    
	    $type_data = array();
	    $type_data_tmp = $this->TryseeTypeTable->listAll(array());
	    foreach ($type_data_tmp as $v){
	        $type_data[$v['id']] = $v;
	    }
	    $this->view->type_data = $type_data;
	}
	
	public function addAction()
	{
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->TryseeTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    
	    $type_data = array();
	    $type_data_tmp = $this->TryseeTypeTable->listAll(array('status'=>1));
	    foreach ($type_data_tmp as $v){
	        $type_data[$v['id']] = $v;
	    }
	    $this->view->type_data = $type_data;
	}
	
	public function doaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    $data['price'] = $this->getParam('price');
	    $data['remark'] = $this->getParam('remark');
	    $data['rank'] = $this->getIntParam('rank');
	    $data['url'] = $this->getParam('url');
	    $data['plus_url'] = $this->getParam('plus_url');
	    $data['type'] = $this->getIntParam('type');
	    $data['icon'] = $this->getParam('icon');
	    if($id > 0){
	        $this->TryseeTable->update($data,$id);
	    }else{
            $data['createdate'] = date('Y-m-d H:i:s');
            $data['status'] = 1;
            $this->TryseeTable->add($data);
	    }
	    $this->redirectToAction();
	    exit();
	}
	
	public function deleteAction()
	{
	    $id = $this->getIntParam('id');
	    $this->TryseeTable->update(array('status'=>0),$id);
	    $this->redirectToAction();
	    exit();
	}
	
	public function ordersAction(){
	    $id = $this->getIntParam('id');
	    $data = $this->TryseeOrderTable->listAll(array('trysee_id'=>$id));
	    $this->view->data = $data;
	}
	
	public function typelistAction(){
	    $where = array('status'=>1);
	    $count = $this->TryseeTypeTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->TryseeTypeTable->listPage($where, 'id ASC', $pageSize, $pageId);
	}
	
	public function typeaddAction(){
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $data = $this->TryseeTypeTable->getRow($id);
	        if($data['id'] > 0){
	            $this->view->data = $data;
	        }
	    }
	}
	
	public function typedoaddAction(){
	    $id = $this->getIntParam('id');
	    $data['title'] = $this->getParam('title');
	    if($id > 0){
	        $this->TryseeTypeTable->update($data,$id);
	    }else{
	        $data['createdate'] = date('Y-m-d H:i:s');
	        $data['status'] = 1;
	        $this->TryseeTypeTable->add($data);
	    }
	    $this->redirectToAction('typelist');
	    exit();
	}
	
	public function typedeleteAction(){
	    $id = $this->getIntParam('id');
	    $this->TryseeTypeTable->update(array('status'=>0),$id);
	    $this->redirectToAction('typelist');
	    exit();
	}
}
